package com.ecity.table{
	import com.ecity.events.GameEvent;
	import com.ecity.util.UIFactory;
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.utils.clearTimeout;
	import flash.utils.setTimeout;
	import gs.TweenLite;
	/**
	 * ...
	 * @author yicheng.zhang
	 */
	public class Poker extends TableDisplayObject {

		private var _amount:int=0;
		private var _back:Sprite;
		private var _backsee:Sprite;
		private var _blank:Sprite;
		private var styleArray:Array = ["club", "heart", "diamond", "spade"];
		private var showPanel:Sprite;
		
		private var _full:Sprite;
		private var _fullTxt:Sprite;
		private var _half:Sprite;
		private var _halfTxt:Sprite;
		
		
		
		public function Poker() {
			this.initChildren("poker");
			initCover();
		}
		
		private function initCover():void {
			_back = this.getLayer("back");
			_back.x = 0;
			_back.y = 0;
			_back.alpha = 0;
			_backsee = this.getLayer("backsee");
			_backsee.x = 0;
			_backsee.y = 0;
			_backsee.alpha = 0;
		}
		
		public function showBack():void {
			_backsee.alpha = 1;
			TweenLite.to(_back, 0.5, { alpha:0, onComplete: cover } );
		}
		
		public function cover():void {
			_backsee.alpha = 1;
			_back.alpha = 1;
		}
		
		public function uncover():void {
			_back.alpha = 0;
			_backsee.alpha = 0;
		}
		
		
		public function showCard(cardIndex:int):void {
			var panel:Sprite = getLayer("cardPanel");
			this.setChildIndex(panel, 0);
			var style:int = int(cardIndex / 13);
			var value:int = cardIndex % 13 + 1;
			var ui:Bitmap =  UIFactory.getBitmapbyName(styleArray[style] + "_" + value);
			var bmpd:BitmapData= ui.bitmapData.clone();
			var card:Bitmap = new Bitmap(bmpd);
			card.y = -0.5 * ui.height;
			if (_amount ==0) {
				card.x = -0.5 * ui.width;
			}else {
				card.x = _amount * 20 + 100-0.5*ui.width;
			}
			panel.addChild(card);
			_amount++;
		}

		private function showPunish(ranking:int):void {
			_full = this.getLayer("full");
			_half = this.getLayer("half");
			_fullTxt = this.getLayer("fullTxt");
			_halfTxt = this.getLayer("halfTxt");
			_full.x = 0;
			_full.y = -50;
			_half.x = 0;
			_half.y = -50;
			_fullTxt.x = 0;
			_fullTxt.y = -50;
			_halfTxt.x = 0;
			_halfTxt.y = -50;
			if (ranking == 0) {
				var stid:uint = setTimeout(hidePunish,2000,stid);
			}else if (ranking == 1) {
				_half.alpha = 1;
				_halfTxt .alpha = 1;
				TweenLite.to(_half, 0.5, {alpha:0,delay:5});
				TweenLite.to(_halfTxt, 0.5, {alpha:0,delay:5,onComplete: hidePunish});
			}else {
				_full.alpha = 1;
				_fullTxt.alpha = 1;
				TweenLite.to(_full, 0.5, {alpha:0,delay:5});
				TweenLite.to(_fullTxt, 0.5, {alpha:0,delay:5,onComplete: hidePunish});
			}
			
		}
		
		public function reStart():void {
			_amount = 0;
			var panel:Sprite = getLayer("cardPanel");
			while (panel.numChildren) {
				panel.removeChildAt(0);
			}
		}
		
		public function hidePunish():void {
			if (arguments.length>0) {
				clearTimeout(arguments[0]);
			}
			dispatchEvent(new GameEvent(GameEvent.PUNISHED));
		}
		
	}

}